import {$get, $post} from '../utils/request';
import {$msg_s, $msg_e} from '../utils/msg';

//获取顾客列表
export const list = async (params) => {
  let res = await $get('GuestRecord/List', params);

  /* res = {
    data: [
      {
        roomId: '101',
        roomType: {
          roomTypeId: '1',
          roomTypeName: '单人间',
          roomTypePrice: 100,
          bedNum: 1,
        },
        roomState: {
          roomStateId: '1',
          roomStateName: '空闲',
        },
        description: '单人间描述',
      },
      {
        roomId: '102',
        roomType: {
          roomTypeId: '2',
          roomTypeName: '双人间',
          roomTypePrice: 200,
          bedNum: 2,
        },
        roomState: {
          roomStateId: '2',
          roomStateName: '维修',
        },
        description: '双人间描述',
      },
      {
        roomId: '103',
        roomType: {
          roomTypeId: '3',
          roomTypeName: '三人间',
          roomTypePrice: 300,
          bedNum: 3,
        },
        roomState: {
          roomStateId: '3',
          roomStateName: '预订',
        },
        description: '三人间描述',
      },
      {
        roomId: '104',
        roomType: {
          roomTypeId: '2',
          roomTypeName: '双人间',
          roomTypePrice: 200,
          bedNum: 2,
        },
        roomState: {
          roomStateId: '1',
          roomStateName: '空闲',
        },
        description: '双人间描述',
      },
      {
        roomId: '105',
        roomType: {
          roomTypeId: '3',
          roomTypeName: '三人间',
          roomTypePrice: 300,
          bedNum: 3,
        },
        roomState: {
          roomStateId: '1',
          roomStateName: '空闲',
        },
        description: '三人间描述',
      },
    ],
    count: 13,
    pageIndex: 1,
    pageSize: 5,
  } */

  return res;
}

//验证添加入参
const checkInput = (params) => {
  if(params.roomTypeId === '0'){
    $msg_e('请选择房间类型');
  }else if(params.roomId === '请选择房间编号'){
    $msg_e('请选择房间编号');
  }else if(!params.guestName){
    $msg_e('请填写顾客姓名');
  }else if(!params.identityId){
    $msg_e('请填写身份证号');
  }else if(params.identityId.length !== 18){
    console.log('params.identityId:');
    console.log(params.identityId);
    $msg_e('身份证号必须是18位');
  }else if(!params.phone){
    $msg_e('请填写手机号');
  }else if(params.phone.length !== 11){
    $msg_e('手机号必须是11位');
  }else if(!params.resideDate){
    $msg_e('请选择入住日期');
  }else if(!params.deposit){
    $msg_e('请填写押金');
  }else if(!(/^\d+$/.test(params.deposit))){
    $msg_e('押金格式有误');
  }else if(!params.guestNum){
    $msg_e('请填写入住人数');
  }else if(!(/^\d+$/.test(params.guestNum))){
    $msg_e('入住人数格式有误');
  }else{
    //入参正确
    return true;
  }

  return false;
}

//添加顾客
export const add = (params) => {
  if(!checkInput(params)){
    //入参错误
    return false;
  }

  let res = $post('GuestRecord/Add', params);

  res = {
    success: true,
    message: '添加顾客成功',
  }

  if(res.success){
    //成功
    $msg_s(res.message);
  }else{
    //失败
    $msg_e(res.message);
  }

  return res.success;
}

//编辑顾客
export const update = (params) => {
  if(!checkInput(params)){
    //入参错误
    return false;
  }

  let res = $post('GuestRecord/Update', params);

  res = {
    success: true,
    message: '编辑顾客成功',
  }

  if(res.success){
    //成功
    $msg_s(res.message);
  }else{
    //失败
    $msg_e(res.message);
  }

  return res.success;
}

//结账
export const checkout = async (params) => {
  const {totalMoney} = await $post('GuestRecord/Checkout', params);

  $msg_s(`结账成功，需支付${totalMoney}元`);
}

//删除顾客
export const del = (params) => {
  let data = $post('GuestRecord/Delete', params);

  data = {
    success: true,
    message: '删除顾客成功',
  }

  const {success, message} = data;

  if(success){
    //成功
    $msg_s(message);
  }else{
    //失败
    $msg_e(message);
  }

  return success;
}